<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\BossCommissionPercent;
use app\airobot\model\UserAssetModel;
use app\airobot\model\UserCommission;
use think\admin\Controller;
use think\facade\Db;
use think\Request;

/**
 * 超管系统-用户管理
 * Class User
 * @package app\airobot\controller
 */
class User extends Controller
{
    private $table = 'user';
    public $commonLogic;

    /**
     * 用户管理
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $userInfo = $this->app->session->get("user");

        $this->title = '用户管理';
        $query = $this->_query($this->table);
        $where = [];
        if (!empty($_GET['id'])) {
            $where['id'] = $_GET['id'];
        }
        $query->where($where);
        $query->like('username,email');
        $query->order('id asc')->page();
    }

    /**
     * 列表数据处理
     * @auth true
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        // 用户总资金、用户持仓资产
        try {
            $userIds = array_column($data, 'id');

            // 用户总充值资金
            $sql = "select `user_id`, sum(usd_amount) as totalRechargeAmount from `recharge_record` where status  = 1 and user_id in (" . implode(',', $userIds) . ")  group by user_id";
            $totalRechargeAmount = Db::query($sql);
            $totalRechargeAmountMap = array_column($totalRechargeAmount, null, 'user_id');

            // 用户当前资金
            $sql = "select `user_id`, sum(balance + git_balance) as crtBalanceAmount from `user_balance` where user_id in (" . implode(',', $userIds) . ")  group by user_id";
            $crtBalanceAmount = Db::query($sql);
            $crtBalanceAmountMap = array_column($crtBalanceAmount, null, 'user_id');

            // 总spin资金
            $sql = "select `user_id`, sum(spin_amount) as totalSpinAmount from `user_spin` where user_id in (" . implode(',', $userIds) . ")  group by user_id";
            $totalSpinAmount = Db::query($sql);
            $totalSpinAmountMap = array_column($totalSpinAmount, null, 'user_id');

            // 发货总资金
            $sql = "select `user_id`, sum(product_amount) as totalProductAmount from `user_spin` where status = 4 and user_id in (" . implode(',', $userIds) . ")  group by user_id";
            $totalProductAmount = Db::query($sql);
            $totalProductAmountMap = array_column($totalProductAmount, null, 'user_id');

            // 用户贡献的利润
            $sql = "select `user_id`, sum(spin_amount - product_amount) as userToSystemProfit from `user_spin` where user_id in (" . implode(',', $userIds) . ")  group by user_id";
            $userToSystemProfit = Db::query($sql);
            $userToSystemProfitMap = array_column($userToSystemProfit, null, 'user_id');

            foreach ($data as &$vo) {
                $vo['totalRechargeAmount'] = isset($totalRechargeAmountMap[$vo['id']]) ? $this->commonLogic->DealAmount($totalRechargeAmountMap[$vo['id']]['totalRechargeAmount']) : '0';
                $vo['crtBalance'] = isset($crtBalanceAmountMap[$vo['id']]) ? $this->commonLogic->DealAmount($crtBalanceAmountMap[$vo['id']]['crtBalanceAmount']) : '0';
                $vo['totalSpinAmount'] = isset($totalSpinAmountMap[$vo['id']]) ? $this->commonLogic->DealAmount($totalSpinAmountMap[$vo['id']]['totalSpinAmount']) : '0';
                $vo['userToSystemProfit'] = isset($userToSystemProfitMap[$vo['id']]) ? $this->commonLogic->DealAmount($userToSystemProfitMap[$vo['id']]['userToSystemProfit']) : '0';
                $vo['totalProductAmount'] = isset($totalProductAmountMap[$vo['id']]) ? $this->commonLogic->DealAmount($totalProductAmountMap[$vo['id']]['totalProductAmount']) : '0';
            }
        } catch (\Exception $exception) {
           $this->error($exception->getMessage());
        }
    }
}